//
//  DSZPostViewController.m
//  7.13苏宁框架
//
//  Created by app01 on 15/7/17.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZPostViewController.h"
#import "DSZMainViewController.h"
#import "AddTopicViewController.h"


@interface DSZPostViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,UITextFieldDelegate>


@end

@implementation DSZPostViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _textField.layer.borderWidth = 1;
    
    _textField.delegate = self;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(count:) name:UITextFieldTextDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTopic:) name:@"topicChange" object:nil];

        
}

-(void)changeTopic:(NSNotification *)notification{
    NSString *string = notification.object;
    _textField.text = [NSString stringWithFormat:@"#%@#",string];
}

-(void)count:(NSNotification *)notification{
    UITextField *filed = notification.object;
    _countLable.text = [NSString stringWithFormat:@"%i/200",filed.text.length];
}

#pragma mark textField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark UIAlertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"postTopic" object:_imageView];
        //[self returnMain:nil];
    }
    _textField.text = @"#话题#";
    _imageView.image = nil;
}


- (IBAction)returnMain:(id)sender {
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DSZMainViewController *vc=[story instantiateViewControllerWithIdentifier:@"main"];
    [self presentViewController:vc animated:NO completion:nil];
    
}


- (IBAction)hotTopic:(UIButton *)sender {
    AddTopicViewController *addVC = [[AddTopicViewController alloc]init];
    [self.navigationController pushViewController:addVC animated:YES];
    
}

- (IBAction)postTopic:(UIButton *)sender {
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否发布" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alter show];
}

- (IBAction)addPicture:(UIButton *)sender {
    UIImagePickerController *imagePk = [[UIImagePickerController alloc] init];
    // 2.判断图库是否可用打开
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        // 3.设置打开图库的类型
        imagePk.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        imagePk.delegate = self;
        
        // 4.打开图片选择控制器
        [self.navigationController presentViewController:imagePk animated:NO completion:nil];
        
    }
    
}



#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //    NSLog(@"%@", info);
    self.imageView.image = info[UIImagePickerControllerOriginalImage];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}



-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
